package com.safe.text;

import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
import com.safe.text.R;
import com.markupartist.android.widget.ActionBar;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SecureActivity extends BaseActivity {

	Button btnFinish;
	Button btnBack;
	EditText etvEmail;
	EditText etvPassword;
	int email = 0;
	StoredData db = new StoredData(this);
	String installDate;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.secure);
		Parse.initialize(this, "tbLiAmAO28YdbVjTaNGJOCHyJgKvRanwIM2pzhQH",
				"jummwB87UtOWbPhhAjiAFoYgHLDB2P92JyuzYrEe");
		currentActivity = this;
		ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setTitle(R.string.app_name);
		final SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		final SharedPreferences.Editor prefsEditor = prefs.edit();
		password = prefs.getString("password", "");
		emailAddress = prefs.getString("emailAddress", "");
		phoneNumber = prefs.getString("phoneNumber", "");
		btnFinish = (Button) findViewById(R.id.btnFinish);
		btnBack = (Button) findViewById(R.id.btnBack);
		etvEmail = (EditText) findViewById(R.id.etv_emailAddress);
		etvPassword = (EditText) findViewById(R.id.etv_password);

		if (prefs.getLong(KEY_FIRST_RUN, 0) != 0) {
			installDate = getDate(prefs.getLong(KEY_FIRST_RUN, 0),
					"MM/dd/yyyy h:mm aa");
		}

		if (password != "") {
			etvPassword.setText(prefs.getString("password", ""));
		}
		if (emailAddress != "") {
			etvEmail.setText(prefs.getString("emailAddress", ""));
		}
		// registers the service that checks for speed
		btnBack.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		btnFinish.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				final Intent finish = new Intent(SecureActivity.this,
						MainActivity.class);
				email = etvEmail.getText().length();
				if (etvPassword.getText().length() == 0) {
					makeToast("Please enter a valid password");
				} else {
					if (BaseActivity.isEmailFormatValid(etvEmail.getText()
							.toString()) == true) {
						isEmailAlreadyRegistered(etvEmail.getText().toString(),
								etvPassword.getText().toString());
						Handler handler = new Handler();
						handler.postDelayed(new Runnable() {
							public void run() {
								if (BaseActivity.registered == false) {
									prefsEditor.putString("emailAddress",
											etvEmail.getText().toString());
									prefsEditor.putString("password",
											etvPassword.getText().toString());
									prefsEditor.putBoolean(KEY_START_ON_BOOT,
											true);
									db.open();
									db.insertUser(etvPassword.getText()
											.toString(), phoneNumber,
											emailAddress);
									db.close();
									ParseUser usersObject = new ParseUser();
									usersObject.setUsername(etvEmail.getText()
											.toString());
									usersObject.setEmail(etvEmail.getText()
											.toString());
									usersObject.setPassword(etvPassword
											.getText().toString());
									usersObject.put("PhoneNumber",
											prefs.getString("phoneNumber", ""));
									usersObject.put("FirstInstallDate",
											installDate);
									usersObject.put("MostRecentInstallDate",
											installDate);
									usersObject.put("isAppOn", true);
									usersObject.put("runAtStartup", true);
									usersObject.put("smsPref", true);
									usersObject
											.signUpInBackground(new SignUpCallback() {
												public void done(
														ParseException e) {
													if (e == null) {
														makeToast("Your account has been successfully registered!");
													} else {
														makeToast("We were unable to register your account : "
																+ e);
													}
												}
											});
									getUserObjectId(etvEmail.getText()
											.toString(), etvPassword.getText()
											.toString());
									prefsEditor.putString("UserId", UserId);
									prefsEditor.commit();
									finish();
									startActivity(finish);
								} else {
									makeToast("This email address is already registered");
								}
							}
						}, 2000);

					} else {
						makeToast("Please enter a valid email");
					}
				}

			}
		});
	}

	public void onResume() {
		currentActivity = this;
		super.onResume();
	}
}
